﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;

namespace UIControls
{
    [ToolboxData("<{0}:TextBox_PostBack runat=server></{0}:TextBox_PostBack>"),
    DefaultProperty("Text")]
    public class TextBox_PostBack : TextBox
    {
        public event EventHandler KeyDownEnter;
        public virtual void onKeyDownEnter(EventArgs arg)
        {
            if (null != KeyDownEnter)
                KeyDownEnter(this, arg);
        }



        protected override void Render(HtmlTextWriter writer)
        {
            Page.VerifyRenderingInServerForm(this);
            Page.ClientScript.GetPostBackEventReference(this, "");            
            this.Attributes.Add("onKeyDown", "if(event.keyCode==13) __doPostBack('" + this.ClientID + "', '');   ");
            this.ID = this.UniqueID;
            base.Render(writer);        



        }

        #region IPostBackDataHandler Members


        
        protected  override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {


            if (postDataKey == this.UniqueID)
            {
                string sNew = postCollection[postDataKey];
                if (sNew != Text )
                {
                    Text = sNew;
                    return true;
                }
            }
            return false;
        }

        protected override void RaisePostDataChangedEvent()
        {
            onKeyDownEnter(new EventArgs());
        }

        #endregion
    }
}
